﻿using System.Linq;
using Microsoft.Xna.Framework.Input;
using RpgXna.Model.XnaClassesWrappers;

namespace RpgXna.Model.Helpers
{



    public delegate void LeftMouseButtonClickEventHandler(MouseState state);
    public delegate void RightMouseButtonClickEventHandler(MouseState state);

    public class InputManager : IInputManager
    {
        private ButtonState _previousMouseLeftButtonState;
        private ButtonState _previousMouseRightButtonState;


        public void Update(MouseState ms, KeyboardStateWrapper kbs)
        {
            ManageLeftMouseClick(ms);
            ManageRightMouseClick(ms);

        }

        void ManageLeftMouseClick(MouseState mState)
        {


            if (mState.LeftButton == ButtonState.Pressed && mState.LeftButton != _previousMouseLeftButtonState)
            {
                OnLeftMouseButtonClick(mState);
            }
            _previousMouseLeftButtonState = mState.LeftButton;
        }

        void ManageRightMouseClick(MouseState mState)
        {
            if (mState.RightButton == ButtonState.Pressed && mState.RightButton != _previousMouseRightButtonState)
            {
                OnRightMouseButtonClick(mState);

            }
            _previousMouseRightButtonState = mState.RightButton;
        }


        public event LeftMouseButtonClickEventHandler LeftMouseButtonClick;
        public event RightMouseButtonClickEventHandler RightMouseButtonClick;

        void OnRightMouseButtonClick(MouseState state)
        {
            RightMouseButtonClickEventHandler handler = RightMouseButtonClick;
            if (handler != null) handler(state);
        }

        void OnLeftMouseButtonClick(MouseState state)
        {
            LeftMouseButtonClickEventHandler handler = LeftMouseButtonClick;
            if (handler != null) handler(state);
        }


    }

}
